package org.quartz.utils;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;

public class DBConnectionManager {

    public static final String DB_PROPS_PREFIX = "org.quartz.db.";

    private static DBConnectionManager instance = new DBConnectionManager();

    private HashMap providers = new HashMap();

    private DBConnectionManager() {
    }

    public void addConnectionProvider(String dataSourceName,
            ConnectionProvider provider) {
        this.providers.put(dataSourceName, provider);
    }

    public Connection getConnection(String dsName) throws SQLException {
        ConnectionProvider provider = (ConnectionProvider) providers
                .get(dsName);
        if (provider == null) {
            throw new SQLException("There is no DataSource named '"
                    + dsName + "'");
        }

        return provider.getConnection();
    }

    public static DBConnectionManager getInstance() {
        // since the instance variable is initialized at class loading time,
        // it's not necessary to synchronize this method */
        return instance;
    }

    public void shutdown(String dsName) throws SQLException {

        ConnectionProvider provider = (ConnectionProvider) providers
        .get(dsName);
        if (provider == null) {
            throw new SQLException("There is no DataSource named '"
                    + dsName + "'");
        }

        provider.shutdown();

    }    
}
